﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder
{
    public partial class Sign : Form
    {
        MedBaseEntities med = new MedBaseEntities();
        public static ObjectSet<User> users;


        public Sign()
        {
            InitializeComponent();
            users = med.Users;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string login = TBLogin.Text;
            if (CheckLogin(login))
            {
                string password = TBPassword.Text;
                if (CheckPassword(password))
                {   

                    string name = TBName.Text;
                    if (!Valid.IsOnlyLetters(name))
                    {
                        ShowError("имени");
                        TBName.Focus();
                    }
                    string surname = TBSurname.Text;
                    if (!Valid.IsOnlyLetters(surname))
                    {
                        ShowError("фамилии");
                        TBSurname.Focus();
                    }
                    string patronymic = TBPatr.Text;
                    if (!Valid.IsOnlyLetters(patronymic))
                    {
                        ShowError("отчества");
                        TBPatr.Focus();
                    }

                    string passport = MTBPassport.Text;
                    if (Valid.CheckPassport(passport, Info2))
                    {
                        User user = new User();
                        user.Login = login;
                        user.Password = password;
                        user.Name = name;
                        user.Surname = surname;
                        user.Patronymic = patronymic;
                        user.Passport = passport;
                        if (button1.Enabled)
                        {
                            this.Hide();
                            ChooseRole fm = new ChooseRole(user);
                            fm.ShowDialog();

                        }
                    }
                }
            }

        }

        private bool CheckLogin(string login)
        {
            login = login.Trim();

            if (login.Equals(string.Empty))
            {
                Info1.Text = "Укажите логин";
                return false;
            }
            if (users.Where(x => x.Login == login).Count() == 0)
                return true;
            else
            {
                Info1.Text = "Таком пользователь уже существует";
                return false;
            }
        }

        private bool CheckPassword(string password)
        {          
            if (password.Length > 4)
            {
                Info1.Text = string.Empty;
                return true;
            }
            else
            {
                Info1.Text = "Пароль должен быть не менее 4 символов";
                return false;
            }

        }

        private void ShowError(string message)
        {
            Info2.Text = "Проверьте корректность ввода " + message;
            button1.Enabled = false;
        }

        private void TBSurname_TextChanged(object sender, EventArgs e)
        {
            if (!Valid.IsOnlyLetters(((TextBox)sender).Text))
                ShowError("имени");
            else
            {
                button1.Enabled = true;
                Info2.Text = String.Empty;
            }
        }

        private void TBName_TextChanged(object sender, EventArgs e)
        {
            if (!Valid.IsOnlyLetters(((TextBox)sender).Text))
                ShowError("фамилии");
            else
            {
                button1.Enabled = true;
                Info2.Text = String.Empty;
            }
        }

        private void TBPatr_TextChanged(object sender, EventArgs e)
        {
            if (!Valid.IsOnlyLetters(((TextBox)sender).Text))
                ShowError("отчества");
            else
            {
                button1.Enabled = true;
                Info2.Text = String.Empty;
            }
        }
    }
}
